package com.plug.controller.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by slaye on 2016/12/28.
 */
public class ConfigUtil {
    /**
     * 读取配置文件
     * @param path  项目全目录/WEB-INF/classes/  + path
     * @return
     */
    public static Properties getProperties(String path){
        path = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1) + path;
        Properties prop = new Properties();
        // 输入流定义
        InputStream inStream = null;
        try{
            // 打开管道
            inStream = new FileInputStream(path);
            prop.load(inStream);
            // 响应value值
            return prop;
        }catch (IOException e){
            e.printStackTrace();
        }finally{
            if (null != inStream){
                try{
                    inStream.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 获取配置文件other.properties中参数key的值
     * @param key
     * @return
     */
    public static String getOtherValue(String key){
        Properties prop = getProperties("config/properties/plug-core.properties");
        if(prop != null){
            return prop.getProperty(key);
        }
        return "";
    }
}
